import { routes, asyncRoutes } from '../../router/index'
import { filterAsyncRoutes } from '../../utils/permission'

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  set_routes (state, addRoutes) {
    state.addRoutes = addRoutes
    state.routes = routes.concat(addRoutes)
  }
}

const actions = {
  generateRoutes ({ commit }, roleId) {
    return new Promise(resolve => {
      const accessedRoutes = filterAsyncRoutes(asyncRoutes, roleId)
      commit('set_routes', accessedRoutes)
      resolve(accessedRoutes)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
